package cn.wen.core.http;

import android.content.Context;
import android.support.v4.util.ArrayMap;

import java.io.File;
import java.util.Map;

import cn.wen.core.http.callback.ICallback;
import cn.wen.core.http.request.IRequest;
import cn.wen.core.http.request.OkHttpRequest;

/**
 * 网络请求的工具类
 */

public class HttpUtils {
    /**
     * 默认使用OkHttp网络请求
     */
    private static IRequest mRequest = new OkHttpRequest();
    /**
     * 请求的URL
     */
    private String mUrl;
    /**
     * 请求的参数
     */
    private Map<String, Object> mParams;
    /**
     * 请求方式，默认是post
     */
    private RequestType mRequestType = RequestType.POST;
    private Context mContext;
    /**
     * 是否缓存
     */
    private boolean mIsCache;
    /**
     * 文件下载的目录名称--只传递名称就好不传也行
     */
    private String mFileDir;
    /**
     * 文件下载名称
     */
    private String mFileName;

    public static final ICallback mDefCallback = new ICallback() {
        @Override
        public void onBefore(Map<String, Object> params) {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onSucceed(String result) {

        }

        @Override
        public void onAfter() {

        }

        @Override
        public void onProgress(float progress, float current, float total) {

        }

        @Override
        public void onSucceed(File file) {

        }
    };


    public HttpUtils(Context context) {
        this.mContext = context;
        mParams = new ArrayMap<>();
    }


    /**
     * 更换网络请求
     */
    public static void changeRequest(IRequest request) {
        mRequest = request;
    }

    /**
     * 设置URL
     */
    public HttpUtils url(String url) {
        this.mUrl = url;
        return this;
    }

    /**
     * 设置参数
     */
    public HttpUtils params(Map<String, Object> params) {
        this.mParams.putAll(params);
        return this;
    }

    public HttpUtils params(String key, Object value) {
        mParams.put(key, value);
        return this;
    }


    /**
     * 是否缓存
     */
    public HttpUtils cache(boolean isCache) {
        this.mIsCache = isCache;
        return this;
    }

    /**
     * 设置请求方式
     */
    public HttpUtils type(RequestType requestType) {
        this.mRequestType = requestType;
        return this;
    }

    /**
     * 文件下载目录名称
     */
    public HttpUtils fileDir(String fileDir) {
        this.mFileDir = fileDir;
        return this;
    }

    /**
     * 文件下载名称
     */
    public HttpUtils fileName(String fileName) {
        this.mFileName = fileName;
        return this;
    }

    /**
     * 执行请求
     */
    public void execute(ICallback callback) {
        if (mRequestType == null) {
            return;
        }
        if (callback == null) {
            callback = mDefCallback;
        }
        // 请求前的一些通用参数的处理
        callback.onBefore(mParams);
        switch (mRequestType) {
            case POST:
                mRequest.post(mContext, mUrl, mParams, callback, mIsCache);
                break;
            case GET:
                mRequest.get(mContext, mUrl, mParams, callback, mIsCache);
                break;
            case UPLOAD:
                mRequest.upload(mContext, mUrl, mParams, callback);
                break;
            case DOWNLOAD:
                mRequest.download(mContext, mUrl, mFileDir, mFileName, callback);
                break;
        }
    }

    /**
     * 执行请求
     */
    public void execute() {
        execute(mDefCallback);
    }

    /**
     * 取消请求
     */
    public void cancel() {
        if (mRequest != null) {
            mRequest.cancel(mContext);
        }
    }

    /**
     * 请求类型：post、get、上传、下载
     */
    public enum RequestType {
        POST, GET, UPLOAD, DOWNLOAD
    }
}
